<template>
  <el-container>
    <div class="header">
      <div class="title">苏小课直播课堂</div>
      <div class="user-info">
        <div class="notification">
          <i class="bell-icon">🔔</i>
          <span class="badge">{{ liveClasses.length }}</span>
        </div>
        <div class="user">您好，{{ userStore.uname }}！</div>
        <div class="actions">
          <a href="./login.html">退出</a>
        </div>
      </div>
    </div>
    <el-header>
      <div class="breadcrumb-nav">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item @click='navigateTo("studentHome/home.html")'>首页</el-breadcrumb-item>
          <el-breadcrumb-item>直播课堂</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </el-header>

    <el-main>
      <div class="content-wrapper">
        <div class="class-list">
          <div v-for="liveClass in liveClasses" :key="liveClass.roomId" class="class-card">
            <h3>{{ liveClass.subject }}</h3>
            <p>开始时间: {{ formatDate(liveClass.createTimestamp) }}</p>
            <p>结束时间: {{ formatDate(liveClass.endTimestamp) }}</p>
            <p>持续时间: {{ liveClass.duration }} 分钟</p>
            <el-button type="primary" @click="joinClass(liveClass)">加入课堂</el-button>
          </div>
        </div>
      </div>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';
import { useUserStore } from "../../store/user.js";
let userStore = useUserStore();
userStore.storeInit();

const liveClasses = ref([]);

const getLiveClasses = () => {
  axios.get("http://localhost:9090/getLiveClasses")
    .then(response => {
      liveClasses.value = response.data;
    })
    .catch(error => {
      console.error("获取直播课堂信息失败", error);
    });
};

const joinClass = (liveClass) => {
  const joinLiveInfo = JSON.stringify({
    createTimestamp: liveClass.createTimestamp,
    endTimestamp: liveClass.endTimestamp,
    duration: liveClass.duration,
    roomId: liveClass.roomId,
    subject: liveClass.subject,
  });
  localStorage.setItem('joinLiveInfo', joinLiveInfo);
  // alert("已成功加入课堂：" + liveClass.subject);
  window.location.href = "/src/pages/Live/login.html"
};

const formatDate = (timestamp) => {
  const date = new Date(timestamp);
  return date.toLocaleString();
};

onMounted(() => {
  getLiveClasses();
});

const navigateTo = (page) => {
  window.location.href = `/src/pages/${page}`;
};
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background-color: #fff;
  border-bottom: 1px solid #ddd;
}

.header .title {
  font-size: 24px;
  color: #1e90ff;
}

.header .user-info {
  display: flex;
  align-items: center;
}

.header .user-info .notification {
  margin-right: 20px;
  position: relative;
}

.header .user-info .notification .badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: red;
  color: #fff;
  border-radius: 50%;
  padding: 2px 5px;
  font-size: 12px;
}

.header .user-info .user {
  margin-right: 10px;
  color: #333;
}

.header .user-info .actions a {
  margin: 0 5px;
  color: #1e90ff;
  text-decoration: none;
}

.el-header {
  background-color: #fff;
  padding: 10px;
  border-bottom: 1px solid #ddd;
}

.breadcrumb-nav {
  padding: 0 20px;
}

.content-wrapper {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.class-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.class-card {
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background-color: #fff;
}

.class-card h3 {
  margin: 0 0 10px;
}

.class-card p {
  margin: 0 0 10px;
}

.el-button {
  margin-top: 10px;
}
</style>
